let url = project + '/onlineArchive/config/outsourcers';
Axios.defaults.baseURL = url;

let main ={
    el:"#app",
    data:function() {
        return {
            list:[],
            loading:false,
            search:{
                pagination:{
                    pageNum: 1,
                    pageSize: 10,
                    total:0,
                },
                keyword:''
            },
            packer:{
                deptName:'',
                remark:''
            },
            packerName:'',
            packerVisible:false,
            rules:{
                deptName:[{ required: true, message: '请输入外包商名称', trigger: 'blur' }]
            },
            valid:[
                {
                    code:1,
                    value:'启用'
                },
                {
                    code:2,
                    value:'禁用'
                }
            ],
            configVisible:false,
            configDivisionList:[],
            divisionList:[],
            packerId: ''
        }
    },
    beforeMount:function(){
        this.getDivisionList();
        this.init();
    },
    computed: {
    },
    methods:{
        init:function(){
            this.initPageList();
        },
        /**
         * 分页获取数据
         */
        getPageList:function(){
            let that = this;
            that.loading = true;
            Axios.post('/pageList',that.search).
            then(function (response) {
                that.loading = false;
                if (response.code != 0){
                    noticeDataRequestFailure(that);
                    return;
                }
                that.list = response.data.list;
                that.search.pagination.total = response.data.total;
            })
            .catch(function (error) {
                noticeFailure(that);
                that.loading = false;
            });
        },
        /**
         * 初始化数据列表，（加载第一页数据）
         */
        initPageList:function(){
            this.tablePageNumChange(1);
        },
        /**
         * 列表 ，页码改变时触发
         * @param pageNum
         */
        tablePageNumChange:function(pageNum){
            this.search.pagination.pageNum = pageNum;
            this.getPageList();
        },
        /**
         * 数据列表，每页条数改变时触发
         * @param pageSize
         */
        tablePageSizeChange:function(pageSize){
            this.search.pagination.pageNum = 1;
            this.search.pagination.pageSize = pageSize;
            this.getPageList();
        },
        newPacker:function(){
            this.packer.deptName = '';
            this.packer.remark ='';
            this.packerVisible = true;
        },
        savePacker:function(){
            let that = this;
            this.$refs.packer.validate(function(valid) {
                if (valid) {
                    Axios.post('/save',that.packer).
                    then(function (response) {
                        if (response.code != 0){
                            noticeDataRequestFailure(that);
                            return;
                        }
                        that.packerVisible = false;
                        that.initPageList();
                        noticeSuccess(that);
                    })
                    .catch(function (error) {
                        noticeFailure(that);
                    });
                } else {
                    return false;
                }
            });
        },
        /**
         * 启用/禁用
         */
        validChange:function(scope){
            let that = this;
            Axios.get('/validChange',{params:{id:scope.row.id}}).
            then(function (response) {
                if (response.code != 0){
                    noticeDataRequestFailure(that);
                    return;
                }
                that.initPageList();
                noticeSuccess(that);
            })
                .catch(function (error) {
                    noticeFailure(that);
                });
        },
        /**
         * 打开配置界面
         */
        openConfigDivision:function(scope){
            let that =this;
            this.packerName = scope.row.deptName;
            this.packerId = scope.row.id;
            Axios.get('/configDivisionList',{params:{id:scope.row.id}}).
            then(function (response) {
                this.configVisible=true;
                if (response.code != 0){
                    noticeDataRequestFailure(that);
                    return;
                }
                that.configDivisionList = response.data;
                that.configVisible = true;
            })
                .catch(function (error) {
                    noticeFailure(that);
                });
        },
        /**
         * 获取分部列表
         * @returns {*}
         */
        getDivisionList:function(){
            let that =this;
            Axios.get('/divisionList').
            then(function (response) {
                this.configVisible=true;
                if (response.code != 0){
                    return;
                }
                that.divisionList = response.data;
            })
            .catch(function (error) {
            });
        },
        move:function(value, direction, movedKeys) {
            let that =this;
            Axios({
                url:'/move',
                method:'post',
                data:{
                    packerId:that.packerId,
                    direction:direction,
                    movedKeys:movedKeys.join(","),
                },
                transformRequest: [function (data) {
                    // 对 data 进行任意转换处理
                    return Qs.stringify(data)
                }],
            }).
            then(function (response) {
                if (response.code != 0){
                    that.rollBack();
                    noticeFailure(that);
                    return;
                }
                else{
                    noticeSuccess(that);
                    return;
                }
            })
                .catch(function (error) {
                    that.rollBack();
                    noticeFailure(that);
                });
        },
        /**
         * 配置失败，回滚
         */
        rollBack(direction,movedKeys){
            if (direction == 'right'){
                for(let i=0;i<movedKeys.length;i++){
                    this.configDivisionList.splice(this.configDivisionList.indexOf(movedKeys[i]+1,1));
                }
            }
            else if(direction == 'left'){
                this.configDivisionList = this.configDivisionList.concat(movedKeys);
            }
        },
        /**
         * 日期列格式化
         * @param row
         * @param column
         * @param cellValue
         * @param index
         */
        dateTimeFormatter:function(row, column, cellValue, index){
            return dateFormatter(cellValue);
        },
        /**
         * 格式化有效性  启用/禁用
         * @param row
         * @param column
         * @param cellValue
         * @param index
         * @returns {string}
         */
        validFormatter:function(row, column, cellValue, index){
            let valid = '';
            for (let i = 0 ; i< this.valid.length ; i++ ){
                if (cellValue == this.valid[i].code){
                    valid = this.valid[i].value;
                    break;
                }
            }
            return valid;
        },
    }
};
new Vue(main);